import Vue from 'vue';
import Router from 'vue-router';

import Example from './modules/example.router';
import HomeRouter from './modules/home.router';
import ShareRouter from './modules/share.router';
import AmwayRouter from './modules/amway.router';
import IdolsRouter from './modules/idols.router';
import MallRouter from './modules/mall.router';
import WalletRouter from './modules/wallet.router';
import UserManageRouter from './modules/user-manage.router';
import OrderRouter from './modules/order.router';
import LoginRouter from './modules/login.router';

Vue.use(Router);

const Main = () => import('@/views/main');


// 示例
/**
 * 路由字段配置说明
 *
 * @param {Object} meta 路由元数据
 *
 * @param {Boolean} meta.unfold 是否可以在菜单栏展开
 * @param {Boolean} meta.hide 是否可以在菜单栏显示
 *
 */
const router = new Router({
    routes: [
        {
            path: '/',
            redirect: 'vote'
        }
    ]
        .concat(
            HomeRouter,
            ShareRouter,
            AmwayRouter,
            MallRouter,
            IdolsRouter,
            WalletRouter,
            UserManageRouter,
            LoginRouter
        )
});

const routerEnterGuard = (to, from, next) => {
    let user = sessionStorage.getItem('user');

    if (to.meta.requireAuth) {
        if (!user) {
            return next({
                path: '/login',
                query: {
                    redirect: to.fullPath
                }
            })
        } else {
            return next();
        }
    } else {
        if (to.path === '/login') {
            if (user) {
                return next({
                    path: to.query.redirect ? to.query.redirect : '/'
                })
            }

            return next();
        }
    }
};

// router.beforeEach(routerEnterGuard);

export default router;
